/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClienteFrame.java
 *
 * Created on 26/02/2009, 05:55:03 PM
 */

package clienteservidor.cliente.gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;

/**
 *
 * @author John Díaz
 */
public class ClienteFrame extends javax.swing.JFrame
{
    private BufferedReader entrada;
    private PrintWriter salida;
    private Socket socket;

    /** Creates new form ClienteFrame */
    public ClienteFrame()
    {
        initComponents();
        mostrar();
        try
        {
            socket = new Socket("localhost", 4090);
            entrada = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            salida = new PrintWriter(socket.getOutputStream(), true);
        }catch (UnknownHostException e)
        {
            System.err.println("Error de tipo UnknownHostException: " + e.getMessage());
            e.printStackTrace();
        }catch (IOException e)
        {
            System.err.println("Error de tipo IOException: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void mostrar()
    {
        setVisible(true);
    }

    public void ocultar()
    {
        setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jspScroll = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jtfTexto = new javax.swing.JTextField();
        jbEnviar = new javax.swing.JButton();
        jbSalir = new javax.swing.JButton();
        jlTitulo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ventana Cliente");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jspScroll.setViewportView(jTextArea1);

        jbEnviar.setText("Enviar");
        jbEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEnviarActionPerformed(evt);
            }
        });

        jbSalir.setText("Salir");
        jbSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalirActionPerformed(evt);
            }
        });

        jlTitulo.setFont(new java.awt.Font("Tahoma", 1, 18));
        jlTitulo.setText("Programa Cliente");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jbEnviar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jlTitulo)
                    .addComponent(jtfTexto, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jspScroll, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jtfTexto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbSalir)
                    .addComponent(jbEnviar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalirActionPerformed
        System.out.println("*** PROGRAMA FINALIZADO ***");
        try
        {
            salida.println("Chao");
            socket.close();
        }catch (IOException e)
        {
            System.err.println("Error de tipo IOExeption: " + e.getMessage());
            e.printStackTrace();
        }
        /*
        catch(InterruptedException e)
        {
            System.err.println("Error de tipo InterruptedException: " + e.getMessage());
            e.printStackTrace();
        }*/
        System.exit(0);
    }//GEN-LAST:event_jbSalirActionPerformed

    private void jbEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEnviarActionPerformed
        String msg = this.jtfTexto.getText();
        if (msg.equals(""))
            JOptionPane.showMessageDialog(null, "No hay mensaje para enviar", "Error", JOptionPane.ERROR_MESSAGE);
        else
        {
            salida.println(msg);
            System.out.println("Se envio el mensaje: \"" + msg + "\" al servidor.");
            try
            {
                String resp = entrada.readLine();
                System.out.println("\nSe recibio el mensaje: \"" + msg + "\" desde el servidor.\n");
                this.jTextArea1.append("Mensaje enviado: " + msg);
                this.jTextArea1.append("\n" + resp + "\n");
            }catch (IOException e)
            {
                System.err.println("Error de tipo IOException: " + e.getMessage());
                e.printStackTrace();
            }
        }
        this.jtfTexto.setText("");
    }//GEN-LAST:event_jbEnviarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton jbEnviar;
    private javax.swing.JButton jbSalir;
    private javax.swing.JLabel jlTitulo;
    private javax.swing.JScrollPane jspScroll;
    private javax.swing.JTextField jtfTexto;
    // End of variables declaration//GEN-END:variables
}